<?php
namespace app\index\controller;
use think\Config;
use think\Controller;
use think\Db;
class Index extends Base
{
    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        // 轮播
        $banner = Db::table('up_banner')
            ->field('banner_id, banner_title, banner_label, banner_image, banner_desc, banner_time,
            banner_type, banner_link')
            ->limit(4)
            ->order('banner_time desc')
            ->select();
        $banner_time = array_column($banner, 'banner_time');
        array_multisort($banner_time,SORT_ASC,$banner);
        foreach($banner as $kk => $vv){
            if(empty($vv['banner_label'])){
                $banner[$kk]['not_null'] = 1;
            }else{
                $banner[$kk]['not_null'] = 2;
            }
        }
        
        // 政策解读
        // 大图
        $big_image = Db::table('up_zhengce')
            ->where('zc_is_big', 2)
            ->field('zc_id, zc_title, zc_describe, zc_label, zc_image, zc_num, zc_commnet_num, zc_is_show_video, 
            zc_time')
            ->order('zc_time desc')
            ->find();
        if($big_image['zc_num'] >= 1000){
            $big_image['zc_num'] = number_format($big_image['zc_num']/1000, 1) . 'k';
        }
        // 小图
        $small_image = Db::table('up_zhengce')
            ->where('zc_is_big', 1)
            ->field('zc_id, zc_title, zc_describe, zc_label, zc_image, zc_num, zc_commnet_num, zc_is_show_video, 
            zc_time')
            ->limit(8)
            ->order('zc_time desc')
            ->select();
        foreach($small_image as $k => $v){
            if($v['zc_num'] >= 1000){
                $small_image[$k]['zc_num'] = number_format($v['zc_num']/1000, 1) . 'k';
            }
        }
        
        // 联盟资讯
        // 首行大图
        $first_big = Db::table('up_information')
            ->field('information_id, information_title, information_desc, information_label, information_image')
            ->where('information_place', 2)
            ->order('information_time desc')
            ->find();
        // 首行小图
        $first_small = Db::table('up_information')
            ->field('information_id, information_title, information_desc, information_label, information_image')
            ->where('information_place', 3)
            ->order('information_time desc')
            ->find();
        // 中间小图
        $second_small = Db::table('up_information')
            ->field('information_id, information_title, information_desc, information_label, information_image')
            ->where('information_place', 5)
            ->order('information_time desc')
            ->find();
        // 中间大图
        $second_big = Db::table('up_information')
            ->field('information_id, information_title, information_desc, information_label, information_image')
            ->where('information_place', 4)
            ->order('information_time desc')
            ->find();
        // 底部大图
        $bottom = Db::table('up_information')
            ->field('information_id, information_title, information_desc, information_label, information_image')
            ->where('information_place', 6)
            ->order('information_time desc')
            ->find();
        
        // 评论
        $comment = Db::table('up_information_comment')
            ->field('comment_id, comment_content, comment_time, comment_information_id')
            ->limit(4)
            ->order('comment_time desc')
            ->select();
        
        $this->assign([
            'banner' => $banner, // banner
            'big_image' => $big_image, // 政策解读大图
            'small_image' => $small_image, // 政策解读小图
            'first_big' => $first_big, // 联盟资讯首行大图
            'first_small' => $first_small, // 联盟资讯首行小图
            'second_small' => $second_small, // 联盟资讯中间小图
            'second_big' => $second_big, // 联盟资讯中间大图
            'bottom' => $bottom, // 联盟资讯底部大图
            'comment' => $comment
        ]);
        return $this->fetch();
    }
    
    /**
     * banner详情
     * @param $id
     * @return mixed
     */
    public function bannerDetail($id){
        $info = Db::table('up_banner')->where('banner_id', $id)->find();
        $this->assign('info', $info);
        return $this->fetch('index/bannerdetail');
    }
    
    /**
     * 政策解读详情
     * @param $id
     * @return mixed
     */
    public function zcDetail($id){
        // 观看数量加1
        Db::table('up_zhengce')->where('zc_id', $id)->setInc('zc_num');
        // 资讯详情
        $info = Db::table('up_zhengce')->where('zc_id', $id)->find();
        // 评论
        $comment = Db::table('up_zhengce_comment')
            ->where('comment_zc_id', $id)
            ->order('comment_time desc')
            ->select();
        // 展示最新的5条资讯评论（右侧）
        $new_comment = Db::table('up_zhengce_comment')
            ->where('comment_zc_id', $id)
            ->order('comment_time desc')
            ->limit(5)
            ->select();
        $denglu = 1;
        if(!session('?user_id')){
            $denglu = 0;
        }
        $this->assign([
            'info' => $info,
            'comment' => $comment,
            'new_comment' => $new_comment,
            'denglu' => $denglu,
        ]);
        return $this->fetch('index/zcdetail');
    }
    
    /**
     * 发布政策解读评论
     * @return \think\response\Json
     */
    public function sendComment(){
        if(!session('?user_id')){
            return json(['status' => 202, 'msg' => '请先登录']);
        }
        $params = input('param.');
        // 评论数量加1
        Db::table('up_zhengce')->where('zc_id', $params['zc_id'])->setInc('zc_commnet_num');
        $user_username = Db::table('up_u_user')->where('user_id',session('user_id'))->value('user_username');
        $insert = [
            'comment_content' => $params['comment_content'],
            'comment_zc_id' => $params['zc_id'],
            'comment_user_name' => $user_username,
            'comment_time' => date('Y-m-d H:i:s'),
            'comment_user_id' => session('user_id')
        ];
        $res = Db::table('up_zhengce_comment')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '发布成功']);
        }else{
            return json(['status' => 0, 'msg' => '发布错误']);
        }
    }
    
    /**
     * 政策列表页面展示
     * @return mixed
     */
    public function zcList(){
        $page = input('page');
        $list = Db::table('up_zhengce')
            ->order('zc_time desc')
            ->page($page, 12)
            ->select();
        foreach($list as $k => $v){
            if($v['zc_num'] >= 1000){
                $list[$k]['zc_num'] = number_format($v['zc_num']/1000, 1) . 'k';
            }
        }
        $this->assign('list', $list);
        return $this->fetch('index/zclist');
    }
    
    /**
     * 更多政策解读---按钮显示
     * @return \think\response\Json
     */
    public function showMore(){
        $count = Db::table('up_zhengce')->count();
        return json(['status' => 200, 'count' => $count]);
    }
    
    /**
     * 更多政策解读
     * @return \think\response\Json
     */
    public function moreZc(){
        $page = input('page');
        $data = Db::table('up_zhengce')
            ->order('zc_time desc')
            ->page($page, 12)
            ->select();
        foreach($data as $k => $v){
            if($v['zc_num'] >= 1000){
                $data[$k]['zc_num'] = number_format($v['zc_num']/1000, 1) . 'k';
            }
        }
        return json(['status' => 200, 'data' => $data]);
    }
    
    /**
     * 联盟资讯详情
     * @param $id
     * @return mixed
     */
    public function informationDetail($id){
        // 观看数量加1
        Db::table('up_information')->where('information_id', $id)->setInc('information_num');
        // 资讯详情
        $info = Db::table('up_information')->where('information_id', $id)->find();
        // 评论
        $comment = Db::table('up_information_comment')
            ->where('comment_information_id', $id)
            ->order('comment_time desc')
            ->select();
        // 展示最新的5条资讯评论（右侧）
        $new_comment = Db::table('up_information_comment')
            ->where('comment_information_id', $id)
            ->order('comment_time desc')
            ->limit(5)
            ->select();
        $denglu = 1;
        if(!session('?user_id')){
            $denglu = 0;
        }
        $this->assign([
            'info' => $info,
            'comment' => $comment,
            'new_comment' => $new_comment,
            'denglu' => $denglu,
        ]);
        return $this->fetch('index/informationdetail');
    }
    
    /**
     * 发布联盟资讯评论
     * @return \think\response\Json
     */
    public function publishComment(){
        if(!session('?user_id')){
            return json(['status' => 202, 'msg' => '请先登录']);
        }
        $params = input('param.');
        // 评论数量加1
        Db::table('up_information')->where('information_id', $params['information_id'])->setInc('information_comment_num');
        $user_username = Db::table('up_u_user')->where('user_id',session('user_id'))->value('user_username');
        $insert = [
            'comment_content' => $params['comment_content'],
            'comment_information_id' => $params['information_id'],
            'comment_user_name' => $user_username,
            'comment_time' => date('Y-m-d H:i:s'),
            'comment_user_id' => session('user_id')
        ];
        $res = Db::table('up_information_comment')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '发布成功']);
        }else{
            return json(['status' => 0, 'msg' => '发布错误']);
        }
    }
    
    /**
     * 联盟资讯列表页面展示-----展示没用到
     * @return mixed
     */
    public function informationList(){
        $page = input('page');
        $list = Db::table('up_information')
            ->order('information_time desc')
            ->page($page, 12)
            ->select();
        foreach($list as $k => $v){
            if($v['information_num'] >= 1000){
                $list[$k]['information_num'] = number_format($v['information_num']/1000, 1) . 'k';
            }
        }
        $this->assign('list', $list);
        return $this->fetch('index/informationlist');
    }
    
    /**
     * 更多政策解读---按钮显示----暂时没用到
     * @return \think\response\Json
     */
    public function showMoreInformation(){
        $count = Db::table('up_information')->count();
        return json(['status' => 200, 'count' => $count]);
    }
    
    /**
     * 更多政策解读------暂时没用到
     * @return \think\response\Json
     */
    public function moreInformation(){
        $page = input('page');
        $data = Db::table('up_information')
            ->order('information_time desc')
            ->page($page, 12)
            ->select();
        foreach($data as $k => $v){
            if($v['information_num'] >= 1000){
                $data[$k]['information_num'] = number_format($v['information_num']/1000, 1) . 'k';
            }
        }
        return json(['status' => 200, 'data' => $data]);
    }
}
